package com.example.hosptial.repository;

import com.example.hosptial.model.view.WardBedPatientView;
import com.example.hosptial.model.view.WardStatusView;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface WardStatusRepository extends JpaRepository<WardStatusView, Integer> {

    // 根据科室ID查询
    List<WardStatusView> findByDeptIdOrderByFloorNumAscWardNumAsc(Integer deptId);

    // 根据科室ID和楼层查询
    List<WardStatusView> findByDeptIdAndFloorNumOrderByWardNumAsc(Integer deptId, Integer floorNum);

    // 查询指定科室的所有楼层
    @Query("SELECT DISTINCT w.floorNum FROM WardStatusView w WHERE w.deptId = :deptId ORDER BY w.floorNum")
    List<Integer> findFloorsByDeptId(@Param("deptId") Integer deptId);

    // 根据楼层查询
    List<WardStatusView> findByFloorNumOrderByWardNumAsc(Integer floorNum);

    // 查询所有空闲床位
    @Query("SELECT w FROM WardStatusView w WHERE w.availableBeds > 0")
    List<WardStatusView> findAvailableBeds();

    // 根据楼层和病房号查询
    List<WardStatusView> findByFloorNumAndWardNum(Integer floorNum, String wardNum);

    @Query("SELECT DISTINCT w.floorNum FROM WardStatusView w WHERE w.deptId = ?1")
    List<Integer> findDistinctFloorNumByDeptId(Integer deptId);

    List<WardStatusView> findByDeptIdAndFloorNumAndAvailableBedsGreaterThan(
        Integer deptId, Integer floorNum, Integer minAvailableBeds);

    List<WardStatusView> findByDeptIdAndFloorNum(Integer deptId, Integer floorNum);
}